 
<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one subdistrict based on id
			get_subdistrict();
			break;
		case 2:
			//get all subdistrict records and return as array
			//generate a json array/object/notation that represents all records in the vaccine table
			get_all_subdistrict();
			break;
		case 3:
			//update_subdistrict($_REQUEST['vn'],$_REQUEST['vs'],$_REQUEST['vu']);
			//update_subdistrict(get_data('vn'),get_datan('vs'),get_data('url'));
			update_subdistrict();		
			break;
		case 4:
			add_subdistrict();
			break;
		case 5:
			search_subdistrict();
			break;
			//get the data from url/request and update the vaccine record
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	
	function get_subdistrict(){
		include_once("subdistrict.php");
		$id=get_datan("id");
		$s=new subdistrict();
		$row=$s->get_subdistrict($id);

		if(!$row){
			echo "{";
				echo jsonn("result",0). ",";
				echo jsons("message","subdistrict not found");
			echo "}";
			return;
		}
		
		echo "{";
				echo jsonn("result",1) .",";
				echo '"subdistrict":{';
				echo jsonn("id",$id).",";
			//name
				echo jsons("subdistrict_name",$row['subdistrict_name']).",";
				echo jsonn("district_id",$row['district_id']);
				// echo jsonn("subdistrict_id",$row['subdistrict_id']);
			echo "}";
		echo "}";
	
	return;
	}


	function get_all_subdistrict(){
	//$id = get_datan("id");
		include_once("subdistrict.php");
		$s=new subdistrict();
		$s->get_all_subdistrict();
		$row=$s->fetch();
		if(!$row){
			echo "{";
				echo jsonn("result",0). ",";
				echo jsons("message","subdistrict not found");
			echo "}";
			return;
		}
		echo "{";
		echo jsonn("result",1).",";
		echo '"subdistrict":';
		$val = Array();
		
		do{
		array_push($val,$row);
		$row =$s->fetch();
		}
		while($row);
		print_r(json_encode($val));
		echo "}";
		}

	function update_subdistrict(){
		include_once("subdistrict.php");
		$s=new subdistrict();
		$id=get_datan('id');
		$subdistrict_name=get_data('sn');
		$district_id=get_datan('did');
		
		if (!$id){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","id not found");
			echo "}";
			return;
		}
		include_once("subdistrict.php");
		$s=new subdistrict();
		$valid=$s->update_subdistrict($id,$subdistrict_name,$district_id);
		if($valid){
			echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","subdistrict updated");
			echo "}";
			}
      else{
          echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","subdistrict not updated");
			echo "}";    	  
		}
	}
	
	

	function add_subdistrict(){
	
		$subdistrict_name=get_data('sn');
		$district_id=get_data('did');
		
		include_once("subdistrict.php");
		$s=new subdistrict();
		$add=$s->add_subdistrict($subdistrict_name,$district_id);
		if($add){
			echo "{";
			echo jsonn("result",1).",";
			echo jsons("message","added successfully");
			echo "}";
		}
		else{
			echo "{";
			echo jsonn("result",0).",";
			echo jsons("message","id not found");
			echo "}";
		}
	}

		function search_subdistrict(){
		$subdistrict_name=get_data('sn');
		include_once("subdistrict.php");
		$s=new subdistrict();
		$row=$s->search_subdistrict($subdistrict_name);
		
		if(!$row){
			echo "{";
				echo jsonn("result",0). ",";
				echo jsons("message","subdistrict not found");
			echo "}";
			return;
		}
		echo "{";
		echo jsonn("result",1).",";
		echo '"subdistrict":';
		$val = Array();
		
		do{
		array_push($val,$row);
		$row =$s->fetch();
		}
		while($row);
		print_r(json_encode($val));
		echo "}";
		}


	

?>